package com.ssl.rms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssl.rms.entity.User;
import com.ssl.rms.entity.UserExample;
import com.ssl.rms.mapper.UserMapper;
import com.ssl.rms.service.UserService;
import com.ssl.rms.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 * 描述：service实现类
 *
 * @author ssl
 * @create 2018/02/06 13:19
 */
@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User get(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (null == user || user.getDelFlag().equals(Constant.DelFlag_F)) {
            logger.warn(MessageFormat.format("user id[{0}]-" + Constant.DelFlag_MSG, id));
            return null;
        }
        return user;
    }

    /**
     * 根据用户名查找用户
     *
     * @param userName
     * @return
     */
    @Override
    public User findByUserName(String userName) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(userName);
        criteria.andDelFlagEqualTo(Constant.DelFlag_F);
        List<User> users = userMapper.selectByExample(example);
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    /**
     * 查询所有
     *
     * @param example 查询条件
     * @return
     */
    @Override
    public List<User> find(UserExample example) {

        return null;
    }

    /**
     * 查询分页后的数据
     *
     * @param page    当前页
     * @param rows    获取的行数
     * @param example 封装后的查询条件
     * @return
     */
    @Override
    public PageInfo<User> findPageInfo(Integer page, Integer rows, UserExample example) {
        PageHelper.startPage(page, rows);
        List<User> users = userMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(users);
        return pageInfo;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public User save(User user) {
        return null;
    }

    /**
     * 选择不为null的字段作为插入数据的字段来插入数据
     *
     * @param user
     * @return
     */
    @Override
    public User saveSelective(User user) {
        return null;
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Override
    public User update(User user) {
        return null;
    }

    /**
     * 选择不为null的字段作为更新数据的字段来更新数据
     *
     * @param user
     * @return
     */
    @Override
    public User updateSelective(User user) {
        return null;
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {

    }
}
